#include "shaderManager.h"


ShaderManager::ShaderManager(ID3D10Device * device, std::string shaderFilePath)
{
	m_pd3dDevice=device;

	// Create the effect
	DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
	dwShaderFlags |= D3D10_SHADER_DEBUG;
#endif


	HRESULT hr=D3DX10CreateEffectFromFile( shaderFilePath.c_str(), NULL, NULL, "fx_4_0", dwShaderFlags, 0,
		m_pd3dDevice, NULL, NULL, &m_pd3dEffect, NULL, NULL );

	if(FAILED(hr))	
		MessageBox(NULL, "Error opening shader file", "Error",0);

}

void ShaderManager::addScalarVariable( std::string name )
{
	if(!m_pd3dEffect)
		return;

	if(m_ScalarVariables[name]!=NULL)
	{
		MessageBox(NULL, "Variable already exists",name.c_str(),0);
		return;
	}

	ID3D10EffectScalarVariable*	variable=m_pd3dEffect->GetVariableByName( name.c_str() )->AsScalar();
	m_ScalarVariables[name]=variable;

}

ID3D10EffectScalarVariable * ShaderManager::getScalarVariableByName( std::string name )
{
	if(m_ScalarVariables[name]==NULL)
	{
		MessageBox(NULL, "Variable doesn't exist",name.c_str(),0);
		return NULL;
	}
	ID3D10EffectScalarVariable * variable = m_ScalarVariables[name];
	return variable;
}

void ShaderManager::addVectorVariable( std::string name )
{
	if(!m_pd3dEffect)
		return;

	if(m_VectorVariables[name]!=NULL)
	{
		MessageBox(NULL, "Variable already exists",name.c_str(),0);
		return;
	}

	ID3D10EffectVectorVariable*	variable=m_pd3dEffect->GetVariableByName( name.c_str() )->AsVector();
	m_VectorVariables[name]=variable;

}

ID3D10EffectVectorVariable * ShaderManager::getVectorVariableByName( std::string name )
{
	if(m_VectorVariables[name]==NULL)
	{
		MessageBox(NULL, "Variable doesn't exist",name.c_str(),0);
		return NULL;
	}
	ID3D10EffectVectorVariable * variable = m_VectorVariables[name];
	return variable;
}

void ShaderManager::addMatrixVariable( std::string name )
{
	if(!m_pd3dEffect)
		return;

	if(m_MatrixVariables[name]!=NULL)
	{
		MessageBox(NULL, "Variable already exists",name.c_str(),0);
		return;
	}

	ID3D10EffectMatrixVariable*	variable=m_pd3dEffect->GetVariableByName( name.c_str() )->AsMatrix();
	m_MatrixVariables[name]=variable;

}

ID3D10EffectMatrixVariable * ShaderManager::getMatrixVariableByName( std::string name )
{
	if(m_MatrixVariables[name]==NULL)
	{
		MessageBox(NULL, "Variable doesn't exist",name.c_str(),0);
		return NULL;
	}
	ID3D10EffectMatrixVariable * variable = m_MatrixVariables[name];
	return variable;
}

void ShaderManager::addResourceVariable( std::string name )
{
	if(!m_pd3dEffect)
		return;

	if(m_ResourceVariables[name]!=NULL)
	{
		MessageBox(NULL, "Variable already exists",name.c_str(),0);
		return;
	}

	ID3D10EffectShaderResourceVariable*	variable=m_pd3dEffect->GetVariableByName( name.c_str() )->AsShaderResource();
	m_ResourceVariables[name]=variable;

}

ID3D10EffectShaderResourceVariable * ShaderManager::getResourceVariableByName( std::string name )
{
	if(m_ResourceVariables[name]==NULL)
	{
		MessageBox(NULL, "Variable doesn't exist",name.c_str(),0);
		return NULL;
	}
	ID3D10EffectShaderResourceVariable * variable = m_ResourceVariables[name];
	return variable;
}

void ShaderManager::setScalarVariable( std::string name, float value )
{
	ID3D10EffectScalarVariable*	variable=m_ScalarVariables[name];
	if(variable==NULL)
	{
		MessageBox(NULL,"Variable doesn't exist",name.c_str(),0);
		return;
	}
	variable->SetFloat(value);
}

void ShaderManager::setVectorVariable( std::string name, float* value )
{
	ID3D10EffectVectorVariable*	variable=m_VectorVariables[name];
	if(variable==NULL)
	{
		MessageBox(NULL,"Variable doesn't exist",name.c_str(),0);
		return;
	}
	variable->SetFloatVector(value);
}

void ShaderManager::setMatrixVariable( std::string name, float* value )
{
	ID3D10EffectMatrixVariable*	variable=m_MatrixVariables[name];
	if(variable==NULL)
	{
		MessageBox(NULL,"Variable doesn't exist",name.c_str(),0);
		return;
	}
	variable->SetMatrix(value);
}

void ShaderManager::setResourceVariable(std::string name, ID3D10ShaderResourceView*   value)
{
	ID3D10EffectShaderResourceVariable*	variable=m_ResourceVariables[name];
	if(variable==NULL)
	{
		MessageBox(NULL,"Variable doesn't exist",name.c_str(),0);
		return;
	}
	variable->SetResource(value);
}

void ShaderManager::addTechnique( std::string name )
{
	
	ID3D10EffectTechnique*  technique=m_pd3dEffect->GetTechniqueByName(name.c_str());

	if(!technique)
	{
		MessageBox(NULL,"Error adding technique", "Error", 0);
		return;
	}

	m_Techniques[name]=technique;

}

ID3D10EffectTechnique* ShaderManager::getTechniqueByName( std::string name )
{
	ID3D10EffectTechnique* technique=m_Techniques[name];

	if(!technique)
	{
		MessageBox(NULL,"The technique doesn't exist", "Error",0);
		return NULL;
	}

	return technique;

}

ShaderManager::~ShaderManager()
{
	if(m_pd3dDevice!=NULL)
	{
		m_pd3dDevice->Release();
		m_pd3dDevice=NULL;
	}

	if(m_pd3dEffect!=NULL)
	{
		m_pd3dEffect->Release();
		m_pd3dEffect=NULL;
	}

}